//
//  EditMyInfoTableViewController.m
//  TravelFollowYourHeart
//
//  Created by appleLJ on 2017/12/11.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "EditMyInfoTableViewController.h"
#import "EditMyInfoTableViewCell.h"
#import "SexView.h"
#import "ChangePasswordViewController.h"

@interface EditMyInfoTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIView *headerView;
@property(nonatomic,strong) UIView *footerView;
@property(nonatomic,strong) NSMutableArray *dataArray;

@property(nonatomic,strong) UIImageView *headImgView;//头像
@property(nonatomic,strong) UILabel *accountLabel;//账号
@property(nonatomic,strong) UITextField *nickNameTextField;//昵称
@property(nonatomic,strong) UILabel *sexLabel;//性别
@property(nonatomic,assign) int sexInt;//1.男 2.女

@end

@implementation EditMyInfoTableViewController(Networking)
- (void)saveEditInfor{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"sex":@(self.sexInt),
                           @"nickname":self.nickNameTextField.text
                           };
    [UserModel intance].sex = [NSNumber numberWithInt:self.sexInt];
    [[HttpRequest shardWebUtil]postNetworkRequestURLString:Url(@"/member/editMember") andParas:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            NSData *data = UIImageJPEGRepresentation(self.headImgView.image, .1);
            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
            [formData appendPartWithFileData:data name:@"avatar" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
    } andTransferGottenData:^(id obj, NSError *error) {
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000) {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
                
                if ([self.sexLabel.text isEqualToString:@"男"]) {
                    self.sexInt = 1;
                }
                if ([self.sexLabel.text isEqualToString:@"女"]) {
                    self.sexInt = 2;
                }
                [UserModel intance].sex = [NSNumber numberWithInteger:self.sexInt];
                [UserModel intance].nickname = self.nickNameTextField.text;
//                [UserModel intance].avatar =
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end

@implementation EditMyInfoTableViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"编辑资料";
    if ([[UserModel intance].sex intValue] != 0) {
        self.sexInt = [[UserModel intance].sex intValue];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
    [self getCellData];
}

- (void)getCellData{
    
}

#pragma makr -- event

- (void)saveBtnEvent{
    //数据保存
    NSLog(@"保存");
    [self saveEditInfor];
}

- (void)selectPoto{
    //选择头像
    [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
        self.headImgView.image = img;
    }];
}

- (void)selectSexHanle{
    //选择性别
    SexView *sexView = [[SexView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_HEIGHT)];
    [sexView show];
    sexView.getSexHanle = ^(NSString *string) {
        self.sexLabel.text = string;
        if ([string isEqualToString:@"男"]) {
            self.sexInt = 1;
        }
        if ([string isEqualToString:@"女"]) {
            self.sexInt = 2;
        }
    };
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditMyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([EditMyInfoTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    
    //线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = getColor(dividerColor);
    if (indexPath.row != 0) {
        [cell.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.top.equalTo(cell.contentView);
            make.height.mas_equalTo(0.5);
        }];
    }
    
    if (indexPath.row != 1 && indexPath.row != 3 && indexPath.row != 4) {
        //箭头
        UIImageView *rightImgView = [[UIImageView alloc] init];
        rightImgView.image = [UIImage imageNamed:@"icon_in"];
        [cell.contentView addSubview:rightImgView];
        [rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(5 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
        }];
    }
    
    if (indexPath.row == 0) {
        //头像
        UIImageView *headImgView = [[UIImageView alloc] init];
        [headImgView sd_setImageWithURL:[NSURL URLWithString:[UserModel intance].avatar] placeholderImage:[UIImage imageNamed:@"list_detail_commend"]];
        headImgView.layer.cornerRadius = 18 / WIDTH_5S_SCALE;
        headImgView.layer.masksToBounds = YES;
        [cell.contentView addSubview:headImgView];
        [headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-25 / WIDTH_5S_SCALE);
            make.width.height.mas_equalTo(36 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
        }];
        self.headImgView = headImgView;
    }
    
    if (indexPath.row == 1) {
        //账号
        UILabel *accountLabel = [[UILabel alloc] init];
        accountLabel.font = DEF_FontSize_13;
        accountLabel.textColor = getColor(textColor);
        accountLabel.textAlignment = NSTextAlignmentRight;
        accountLabel.text = [UserModel intance].phone;
        
        [cell.contentView addSubview:accountLabel];
        [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
        }];
        self.accountLabel = accountLabel;
    }
    
    if (indexPath.row == 3) {
        //昵称
        UITextField *nickNameTextFiled = [[UITextField alloc] init];
        nickNameTextFiled.font = DEF_FontSize_13;
        nickNameTextFiled.textColor = getColor(textColor);
        nickNameTextFiled.textAlignment = NSTextAlignmentRight;
        nickNameTextFiled.text = [UserModel intance].nickname;
        [cell.contentView addSubview:nickNameTextFiled];
        [nickNameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(100 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(40 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
        }];
        self.nickNameTextField = nickNameTextFiled;
    }
    
    if (indexPath.row == 4) {
        //性别
        UILabel *sexLabel = [[UILabel alloc] init];
        sexLabel.font = DEF_FontSize_13;
        sexLabel.textColor = getColor(textColor);
        sexLabel.textAlignment = NSTextAlignmentRight;
        if ([[UserModel intance].sex intValue] == 1) {
            sexLabel.text = @"男";
        }
        if ([[UserModel intance].sex intValue] == 2) {
            sexLabel.text = @"女";
        }
        [cell.contentView addSubview:sexLabel];
        [sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
        }];
        self.sexLabel = sexLabel;
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        //选择头像
        [self selectPoto];
    }else if (indexPath.row == 2){
        //修改密码
        NSLog(@"修改密码");
        ChangePasswordViewController * changePasswordVC = [[ChangePasswordViewController alloc] init];
        [self.navigationController pushViewController:changePasswordVC animated:YES];
    }else if (indexPath.row == 3){
        //修改昵称
        NSLog(@"修改昵称");
    }else if (indexPath.row == 4){
        //修改性别
        NSLog(@"修改性别");
        [self selectSexHanle];
    }
}

#pragma mark -- init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[EditMyInfoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([EditMyInfoTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8)];
        _headerView.backgroundColor = getColor(bgColor);
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT - 64 - 8 - 44 * 5 / WIDTH_5S_SCALE)];
        _footerView.backgroundColor = getColor(bgColor);
        //保存按钮
        UIButton *saveButton = [[UIButton alloc] init];
        saveButton.titleLabel.font = DEF_FontSize_14;
        [saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [saveButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [saveButton setBackgroundColor:getColor(mainColor)];
        saveButton.layer.cornerRadius = 18 / WIDTH_5S_SCALE;
        saveButton.layer.masksToBounds = YES;
        saveButton.userInteractionEnabled = YES;
        [saveButton addTarget:self action:@selector(saveBtnEvent) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:saveButton];
        [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_footerView).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(_footerView).offset(-10 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(36 / WIDTH_5S_SCALE);
            make.bottom.equalTo(_footerView).offset(-100 / WIDTH_5S_SCALE);
        }];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@{@"title":@"头像"},
                       @{@"title":@"账号"},
                       @{@"title":@"修改密码"},
                       @{@"title":@"昵称"},
                       @{@"title":@"性别"},
                       ].mutableCopy;
    }
    return _dataArray;
}

@end
